<template>
  <div class="dashboard-container">
    <div class="app-container">
      <template>
        <div class="dashboard-container">
          <div class="app-container">
            <el-card>
              <el-tabs>
                <!-- 放置页签 -->
                <el-tab-pane label="角色管理">
                  <!-- 新增角色按钮 -->
                  <el-row style="height:60px">
                    <el-button
                      icon="el-icon-plus"
                      size="small"
                      type="primary"
                      @click="addRoleLs"
                    >新增角色</el-button>
                  </el-row>

                  <!-- 对话框 -->
                  <el-dialog
                    :title="roleData.id?'编辑':'新增'"
                    :visible="dialogVisible"
                    width="50%"
                    @close="btnCancel"
                  >
                    <el-form ref="roleData" label-width="120px" :model="roleData" :rules="rules">
                      <el-form-item label="角色名称" prop="name">
                        <el-input v-model="roleData.name" style="width:80%" placeholder="1-10个字符" />
                      </el-form-item>
                      <el-form-item label="角色描述" prop="description">
                        <el-input v-model="roleData.description" style="width:80%" placeholder="1-10个字符" />
                      </el-form-item>
                    </el-form>
                    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
                    <el-row slot="footer" type="flex" justify="center">
                      <!-- 列被分为24 -->
                      <el-col :span="6">
                        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
                        <el-button size="small" @click="btnCancel">取消</el-button>
                      </el-col>
                    </el-row>
                  </el-dialog>

                  <!-- 表格 -->
                  <el-table border="" :data="RoleList">
                    <el-table-column label="序号" width="120" type="index" />
                    <el-table-column label="角色名称" width="240" prop="name" />
                    <el-table-column label="描述" prop="description" />
                    <el-table-column label="操作">
                      <template #default="{row}">
                        <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                        <el-button size="small" type="primary" @click="redactRole(row.id)">编辑</el-button>
                        <el-button size="small" type="danger" @click="delFn(row.id)">删除</el-button>
                      </template>

                    </el-table-column>
                  </el-table>
                  <!-- 分页组件 -->
                  <el-row type="flex" justify="center" align="middle" style="height: 60px">
                    <!-- 分页组件 -->
                    <el-pagination
                      layout="prev,pager,next"
                      :page-size="page.pagesize"
                      :total="page.total"
                      @current-change="currentChange"
                    />
                  </el-row>
                </el-tab-pane>
                <el-tab-pane label="公司信息">
                  <el-alert
                    title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
                    type="info"
                    show-icon
                    :closable="false"
                  />
                  <el-form label-width="120px" style="margin-top:50px">
                    <el-form-item label="公司名称">
                      <el-input v-model="companyInfo.name" disabled style="width:400px" />
                    </el-form-item>
                    <el-form-item label="公司地址">
                      <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
                    </el-form-item>
                    <el-form-item label="邮箱">
                      <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
                    </el-form-item>
                    <el-form-item label="备注">
                      <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
                    </el-form-item>
                  </el-form>
                </el-tab-pane>

              </el-tabs>
            </el-card>
          </div>
        </div>
      </template>

      <el-dialog title="分配权限" :visible="showPerm" @close="btnPermCancel">
        <!--
          default-expand-all 默认展开所有
          show-checkbox显示多选框
          check-strictly 断开父子节点的选项关联
         -->
        <el-tree
          ref="permTree"
          node-key="id"
          :data="permList"
          :props="{label: 'name'}"
          default-expand-all
          show-checkbox
          check-strictly
        />
        <template #footer>
          <el-row type="flex" justify="center">
            <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-row>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, delRole, addRole, getRoleDetail, updateRole, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permisson'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      companyInfo: {}, // 企业信息
      page: { // 页码
        page: 1,
        pagesize: 2,
        total: 0
      },
      RoleList: [], // 角色列表
      dialogVisible: false, // 显示对话框

      roleData: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 10, message: '长度不能超过十位', trigger: 'blur' }],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { max: 50, message: '长度不能超过 50 位', trigger: 'blur' }
        ]
      },
      showPerm: false, // 控制权限弹窗
      permList: [], // 权限列表
      roleId: ''
    }
  },
  created() {
    this.getCompanyInfo()
    this.getRoleList(this.page)
    this.getPermList()
  },
  methods: {
    async getCompanyInfo() {
      // 根据id 请求企业信息
      this.companyInfo = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
    },
    async getRoleList() {
      // 获取角色列表
      const { rows, total } = await getRoleList(this.page)
      this.RoleList = rows
      this.page.total = total
    },
    currentChange(newPage) {
      // 次方法可以拿到用户点击的页数
      this.page.page = newPage
      this.getRoleList()
    },

    // 删除
    async delFn(id) {
      try {
        // 询问
        await this.$confirm('是确认删除！')
        // 发请求
        await delRole(id)
        // 提示成功
        this.$message.success('删除成功')
        // 更新页面
        if (this.RoleList.length === 1 && this.page.page > 1) {
          this.page.page -= 1
        }
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },

    // 新增
    addRoleLs() {
      this.dialogVisible = true
    },

    // 编辑
    async redactRole(id) {
      this.dialogVisible = true
      this.roleData = await getRoleDetail(id)
    },

    // 确认新增
    async btnOk() {
      try {
        // 校验数据
        await this.$refs.roleData.validate()
        // 发送请求
        if (this.roleData.id) {
        // 编辑
          await updateRole(this.roleData)
        } else {
        // 新增
          await addRole(this.roleData)
        }

        // 提示用户
        this.$message.success('添加成功')
        // 更新页面
        this.getRoleList()
        // 关闭窗口
        this.dialogVisible = false
      } catch (error) {
        console.log(error)
      }
    },
    // 取消新增
    btnCancel() {
      // 清理数据
      this.roleData = {
        name: '',
        description: ''
      }
      // 清理报错
      this.$refs.roleData.resetFields()
      // 关闭窗口
      this.dialogVisible = false
    },
    // 权限分配弹窗
    async assignPerm(id) {
      this.roleId = id
      const { permIds } = await getRoleDetail(id)
      this.showPerm = true

      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds)
      })
      // 获取权限 树形显示
      // this.permList = listToTreeData(await getPermissionList(), '0')
    },
    // 获取权限 树形显示
    async getPermList() {
      this.permList = listToTreeData(await getPermissionList(), '0')
    },
    // 取消分配权限
    btnPermCancel() {
      this.$refs.permTree.setCheckedKeys([])
      this.showPerm = false
    },
    // 确定分配权限
    async btnPermOK() {
      // 发请求
      await assignPerm({
        id: this.roleId,
        permIds: this.$refs.permTree.getCheckedKeys()
      })
      // 提示用户
      this.$message.success('提交成功')
      // 关闭弹窗
      this.showPerm = false
    }
  }
}
</script>

<style>

</style>
